Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • extract the variable name out of a local `weight'

    I am currently working on an ado-file. After running the ado, the weight is returned in the local `weight'. This works fine.
    However, I also need to extract the variable name.

    Code:
    local weight = "[iweight= V016]"
    So far, I use this syntax, to create a second local (weight2):

    Code:
    local weight2 = substr("`weight'", -5, .)
    local weight2: subinstr local weight2 "]" "", all
    di "`weight2'"
    However, using "-5" to extract the last characters night lead to problems. Maybe there is another way? Could I use "ustrregexs" to extract the letter "V" and the 3 numbers instead? How?

    Thanks, Simon

  • #2
    I would back up here and go upstream from where I guess you are.

    If a user is asking for weights, then how you allow that using syntax lets you separate the type of weight and the expression defining them.

    Given the auto data, this example cuts out any particular command and exploits the fact that the syntax parser works on local macro 0 (which is automagically produced when you type a command as whatever follows that command).

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . local 0 [iweight=mpg]
    
    
    . syntax [iweight/]
    
    . mac li
    
    _weight:        iweight
    _exp:           mpg
    _0:             [iweight=mpg]

    I have edited out output irrelevant to the point.

    Moral: Let syntax do this for you.

    Comment


    • #3
      Hello Nick, thanks for your reply.

      I have written an ado-file to build several tables (with collect/table), arrange them and print them in word, using putdocx/putexcel. When the data is weighted, we usually use iweight.

      Code:
      * syntax varlist [if] [fw aw iw], ROW(varlist) [same] [frno] [fra(varname)] [round100]
      Therefore the returned local `weight' basically equals:

      Code:
      * local weight = "[iweight= V016]"
      I recently added an option [round100], where I need "contract". However, it allows fweights only. Thats the reason why I wanted to extract the name of the weight (variable), to use it in the option (only), if needed.

      My workaround so far, is:

      Code:
      local weight2 = "`weight'"
      
      local weight2 = substr("`weight'", -5, .)
      local weight2: subinstr local weight2 "]" "", all
      
      gen weight_2 = `weight2'
      
      contract var [fweight= weight_2],  percent(newp) nomiss
      However, I would prefer another solution.

      Comment


      • #4
        I think that I have given you a solution.

        Comment


        • #5
          Originally posted by Simon Pfaff View Post
          I have [...]
          Code:
          * syntax varlist [if] [fw aw iw], ROW(varlist) [same] [frno] [fra(varname)] [round100]
          Therefore the returned local `weight' basically equals:

          Code:
          * local weight = "[iweight= V016]"
          No, it does not. The local weight equals iweight and the local exp equals = V016. Thus,

          Code:
          syntax ... [fw aw iw]
          already separates the weight's name from the expression that defines it. The latter might be a single variable, such V016; it might well be a complex expression with nested functions, so you probably do not want to extract the variable name(s) only.

          I won't comment further on using iweights as a quasi-default but if you want that, you want to list iweight first, as in

          Code:
          syntax ... [iw fw aw]
          Last edited by daniel klein; Yesterday, 11:16.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            I think that I have given you a solution.
            Ok, thanks, again, thats true, I get it now, this makes things a lot easier.

            Originally posted by daniel klein View Post

            No, it does not. The local weight equals iweight and the local exp equals = V016.
            Thanks a lot for pointing that out, Daniel, I missed that / got that wrong.

            Originally posted by daniel klein View Post
            I won't comment further on using iweights as a quasi-default but if you want that,
            You are right, I should probably rethink that...
            Last edited by Simon Pfaff; Today, 01:05.

            Comment

            Working...
            X